package com.parent;

import com.aliyun.oss.OSS;
import com.parent.api.aliyun.OSSUtils;
import com.parent.api_entity.aliyun.OssUrl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @Author yan
 * @Date 2023/6/28 0028 15:08
 * @Description
 */
public class OSSDemo {
/*
    接口实现demo
   @PostMapping("/downloads")
    @SysLog(title = "文件下载")
    public void downloads(HttpServletResponse response,
                          @RequestParam String url) throws IOException {

//        String s = "http://xxxx.oss-cn-beijing.aliyuncs.com/2023-06-28/R-C1.jpg";
//        int firstIndex = url.indexOf('/', url.indexOf('/') + 1);
//        int secondIndex = url.indexOf('/', firstIndex + 1);
//        String result = url.substring(firstIndex + 1, secondIndex);
//        String imageBucketName = result.substring(0, result.indexOf("."));
//        String ossEndpoint = result.substring(result.indexOf(".") + 1, result.length());
//
//        int Index = url.indexOf('/', url.indexOf('/') + 2);
//        String filePath = url.substring(Index + 1, url.length());

        OssUrl ossUrl = new OssUrl(url);
        String filePath = ossUrl.getFilePath();
        String bucketName = ossUrl.getBucketName();
        String endpoint = ossUrl.getEndpoint();

        OSS ossClient = OSSUtils.createOss(accessKeyId, accessKeySecret, endpoint);
        // java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64
        //解决 下载commons-codes 作为项目资源
        // api-tools 模块下 main/lib/comons-codec-{版本}.jar 作为该项目的资源
        boolean flag = ossClient.doesBucketExist(bucketName);
        if (flag) {
            ByteArrayOutputStream outputStream = OSSUtils.downloadOss(ossClient, bucketName, filePath);
            byte[] bytes = outputStream.toByteArray();
            ServletOutputStream out = response.getOutputStream();
            out.write(bytes);
        }

    }*/
}
